package ng.test;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import lib.Config;
import ng.graphics.Camera;
import ng.lib.Vector2D;
import ng.map.Brick;
import ng.map.Map;
import ng.map.MapObjectDynamic;
import ng.map.MapObjectStatic;
import ng.map.Rocket;
import ng.map.Spacecraft;
import client.gui.DisplayAble;
import client.gui.ingame.Screen;

public class FullScreen extends DisplayAble{

	public final static int TICK_DURING = 20;
	private final Map map;
//	private final Camera cam4;
	private final Camera cam1;
	private final Camera cam2;
	private final Camera cam3;
	
	
	public FullScreen() {
		this.map = new Map(1024,768);
		this.cam1 = new Camera(map, new Rectangle(0,0,512,500));
		this.cam2 = new Camera(map, new Rectangle(512,0,512,500));
		this.cam3 = new Camera(map, new Rectangle(200,150,500,400));
		this.cam3.setZoom(0.5);
//		this.cam4 = new Camera(map, new Rectangle(200,200,800,600));

		final MapObjectDynamic o1 = new Spacecraft(new Vector2D(100, 100));
		final MapObjectDynamic o2 = new Spacecraft(new Vector2D(200, 200));
		//final MapObjectDynamic o3 = new Spacecraft(new Vector2D(600, 200));
		final MapObjectStatic o4 = new Brick(new Vector2D(500,380));
		
		this.map.addMapObject(o1);
		this.map.addMapObject(o2);
//		this.map.addMapObject(o3);	
		this.map.addMapObject(o4);	
		
		this.cam1.setHaunted(o1);
		this.cam2.setHaunted(o2);
		this.cam3.setHaunted(o4);
		
		setKeyAdapter(new KeyAdapter() {

			
			@Override
			public void keyPressed(KeyEvent e) {
				int k = e.getKeyCode();
				// System.out.println(k);

				switch (k) {
				case 65:// a
					o1.setAngularRate(-0.1);
					break;
				case 87:// w
					o1.getAcceleration().add(new Vector2D(0,-1).rotate(o1.getAngle()));
					break;
				case 68:// d
					o1.setAngularRate(0.1);
					break;
				case KeyEvent.VK_LEFT:
					o2.setAngularRate(-0.1);
					break;
				case KeyEvent.VK_UP:// w
					o2.getAcceleration().add(new Vector2D(0,-1).rotate(o2.getAngle()));
					break;
				case KeyEvent.VK_RIGHT:// d
					o2.setAngularRate(0.1);
					break;
				case KeyEvent.VK_ENTER:// d
					MapObjectDynamic p = new Rocket(o2.getPosition().clone());
					p.setSpeed(new Vector2D(0,-1).rotate(o2.getAngle()).multiply(8));
					map.addMapObject(p);
					break;
				case KeyEvent.VK_S:// d
					MapObjectDynamic p2 = new Rocket(o1.getPosition().clone());
					p2.setSpeed(new Vector2D(0,-1).rotate(o1.getAngle()).multiply(8));
					map.addMapObject(p2);
					break;
				case KeyEvent.VK_ESCAPE:
					exit();
				

				}

			}

			@Override
			public void keyReleased(KeyEvent e) {
				int k = e.getKeyCode();
				// System.out.println(k);

				switch (k) {
				case 65:// a
					o1.setAngularRate(0);
					break;
				case 87:// w
					o1.getAcceleration().x = 0;
					o1.getAcceleration().y = 0;
					break;
				case 68:// d
					o1.setAngularRate(0);
					break;
				case KeyEvent.VK_LEFT:
					o2.setAngularRate(0);
					break;
				case KeyEvent.VK_UP:// w
					o2.getAcceleration().x = 0;
					o2.getAcceleration().y = 0;
					break;
				case KeyEvent.VK_RIGHT:// d
					o2.setAngularRate(0);
					break;

			
				}
			}
		});
	}



	public static void main(String[] args) {
		new FullScreen();
	}

	@Override
	synchronized public Graphics2D draw(Graphics2D g) {
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g.setColor(Color.white);
		g.fillRect(0, 0, Config.getInstance().xRes, Config.getInstance().yRes);
		
		//cam1.tick();
		//cam2.tick();
		cam3.tick();
		map.tick();
		//cam1.draw(g);
		//cam2.draw(g);
		cam3.draw(g);
		
		try {
			Thread.sleep(TICK_DURING);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return g;
		
	}






}
